<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/4/20
 * Time: 14:04
 */

namespace app\shop\api;

use app\common\api\BaseApi;
use app\shop\model\ProductQuantityHis;
use app\shop\model\ProductSku;

class ProductQuantityHisApi extends BaseApi{

    const ADD             = "shop/ProductQuantityHisApi/add";
    const SAVE            = "shop/ProductQuantityHisApi/save";
    const GET_INFO        = "shop/ProductQuantityHisApi/getInfo";
    const QUERY_NO_PAGING = "shop/ProductQuantityHisApi/queryNoPaging";
    const QUERY           = "shop/ProductQuantityHisApi/query";
    /**
     * 批量添加
     */
    const ADD_ALL = "shop/ProductQuantityHisApi/addAll";

    protected function _init(){
        $this -> model = new ProductQuantityHis();
    }

    public function addAll($entity){
// dump($entity);exit;
// array(1) {
//   [0] => array(10) {
//     ["change"] => int(-1)
//     ["pid"] => string(1) "2"
//     ["create_time"] => int(1467254086)
//     ["change_time"] => int(1467254086)
//     ["operator_uid"] => int(0)
//     ["operator_name"] => string(6) "系统"
//     ["notes"] => string(6) "售出"
//     ["sku_id"] => string(0) ""
//     ["left_quantity"] => int(499)
//     ["dtree_type"] => int(6076)
//   }
// }
        $len = count($entity);//数据个数
        if($len == 0) $this -> apiReturnSuc("库存变动成功");

        $skuModel = new ProductSku();
        $skuModel -> startTrans();//开启事务
        $error    = false;
        //$sku_entity = array();
        foreach($entity as $k => $v){
            $pid           = $v['pid'];
            $sku_id        = $v['sku_id'];
            $left_quantity = $v['left_quantity'];
            $map = [
                'product_id' => $pid,
                'sku_id' => $sku_id,
            ];
            $sku_entity = [
                'quantity' => $left_quantity,
            ];
            $result = $skuModel -> create($sku_entity,2);
            if($result === false){
                $error = $skuModel -> getError();
            }
            if($error === false){
                $result = $skuModel -> where($map) -> save();
            }
            if($result === false){
                $error = $skuModel -> getDbError();
            }
            if($error !== false){
                break;
            }
        }
        if($error === false){
            if($len == 1){
                $result = $this -> model -> create($entity[0],1);
                if($result === false){
                    $error = $this -> model -> getError();
                }
                if($error === false){
                    $result = $this -> model -> add();
                }
                if($result === false){
                    $error = $this -> model -> getDbError();
                }
            }else{
                $result = $this -> model -> addAll($entity);
                if($result === false){
                    $error = $this -> model -> getDbError();
                }
            }
        }
        if($error === false){
            $skuModel -> commit();
            return $this -> apiReturnSuc("库存变动成功");
        }else{
            $skuModel -> rollback();
            return $this -> apiReturnErr($error);
        }
    }

}